package com.hjq.mallcart.controller;

import com.alibaba.fastjson.JSON;
import com.hjq.common.constants.AuthConstants;
import com.hjq.common.constants.CartConstants;
import com.hjq.mallcart.entity.Cart;
import com.hjq.mallcart.entity.CartItem;
import com.hjq.mallcart.entity.UserInfoVO;
import com.hjq.mallcart.intercepter.CartIntercepter;
import com.hjq.mallcart.service.CartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.concurrent.ExecutionException;

/**
 * @auther HJQ
 */
@Controller

public class CartController {
   @Autowired
    CartService catService;

    /**
     *cookie有一个userKey标识信息
     * 如果第一次使用购物车，都会给一个临时用户身份，浏览器保存，每次访问都是带上
     * 登录：session里有
     * 没登陆：userKey
     * 如果没有临时用户：帮忙创建临时用户changeCount.html?
     */
    @ResponseBody
    @RequestMapping("/getCartItems")
    public List<CartItem> getCurrentUserCart(@RequestParam("memberId") Long memberId) {
        List<CartItem> cartItems= catService.getCurrentUserCart(memberId);

        return cartItems;

    }
    @RequestMapping("/deleteItem.html")
    public String deleteItem(@RequestParam("skuId") Long skuId) {
        catService.deleteItem(skuId);
        return "redirect:http://cart.hjqmall.com/cart.html";

    }
    @RequestMapping("/changeCount.html")
    public String changeCount(@RequestParam("skuId") Long skuId,
                        @RequestParam("num") Integer num) {
        catService.changeCount(skuId,num);
        return "redirect:http://cart.hjqmall.com/cart.html";

    }
    @RequestMapping("/checkItem.html")
    public String check(@RequestParam("skuId") Long skuId,
                        @RequestParam("checked") Integer checked) {
        catService.checkItem(skuId,checked);
        return "redirect:http://cart.hjqmall.com/cart.html";

    }
    @RequestMapping("/cart.html")
    public String cart(Model model) throws ExecutionException, InterruptedException {
        UserInfoVO currentUserInfoVO = CartIntercepter.threadLocal.get();
        System.out.println(currentUserInfoVO);
        Cart cartItems= catService.getAllCart();
        model.addAttribute(CartConstants.CART,cartItems);

        return  "cartList";
    }
    @RequestMapping("/addItem.html")
    public String addTOcart(@RequestParam("skuId") Long skuId,
                            @RequestParam("number" )Integer number,
                            Model model,
                            RedirectAttributes redirectAttributes) throws ExecutionException, InterruptedException {
        CartItem cartItem=catService.addItem(skuId,number);
        redirectAttributes.addAttribute("skuId",skuId);
        //redirectAttributes.addFlashAttribute(CartConstants.CART_ITEM, JSON.toJSONString(cartItem));

        return  "redirect:http://cart.hjqmall.com/addItem/success.html";
    }
    @RequestMapping("/addItem/success.html")
    public String addTOcart(@RequestParam("skuId")Long skuId,Model m4) {
        CartItem cartItem= catService.getCartItem(skuId);
        m4.addAttribute(CartConstants.CART_ITEM,cartItem);

        return  "success";
    }


}
